package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.pojo.NursingLevel;
import com.zzyl.nursing.domain.vo.NursingLevelVo;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.service.INursingLevelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理等级Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    //定义redis的key
    private static final String CACHE_KEY_PREFIX ="nursingLevel:all";

@Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        int i = nursingLevelMapper.insert(nursingLevel);
        redisTemplate.delete(CACHE_KEY_PREFIX);

        return i;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int i = nursingLevelMapper.updateById(nursingLevel);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return i;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        int i = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return i;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        int i = nursingLevelMapper.deleteById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return i;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }
    /**
     * 查询所有护理等级
     *
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> getAll() {
        //一、首先从缓存中查询，如果存在则返回
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        //根据key获取值
        String resultJson = ops.get(CACHE_KEY_PREFIX);
        //判断缓存中是否存在
        if(StrUtil.isNotBlank(resultJson)){
            //将json数组转化为集合
            return JSONUtil.toList(resultJson,NursingLevelVo.class);
        }
        //二、从缓存中查询，如果不存在则查询表并放入缓存中
        List<NursingLevel> NusingLevelList = this.lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        //转化对象NursingLevelVo
        List<NursingLevelVo> nursingLevelVoList = BeanUtil.copyToList(NusingLevelList, NursingLevelVo.class);
        //查询表并放到存储到缓存中
       ops.set(CACHE_KEY_PREFIX,JSONUtil.toJsonStr(NusingLevelList));
return nursingLevelVoList;
    }

}
